package net.gz01.wallet.web.controller.admin;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.TopBank;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.YouliAccount;

public class YouliAccountController extends BaseController{

	public void search(){
		render("basedata/youliAccountList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<YouliAccount> pageResult = YouliAccount.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addPage(){
		setAttr("topBankList", TopBank.topBankList);
		render("basedata/youliAccountAdd.html");
	}

	public void updatePage(){
		YouliAccount youliAccount = YouliAccount.dao.findById(getPara("id"));
		setAttr("youliAccount", youliAccount);
		setAttr("topBankList", TopBank.topBankList);
		render("basedata/youliAccountUpdate.html");
	}

	@Before(Tx.class)
	public void add(){
		YouliAccount youliAccount = getModel(YouliAccount.class);
		youliAccount.setBankName(TopBank.getByShortEnName(youliAccount.getBankEnglishLetters()).getBankName());
		youliAccount.save();
		renderSuccess();
	}

	@Before(Tx.class)
	public void update(){
		YouliAccount youliAccount = getModel(YouliAccount.class);
		youliAccount.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete(){
		YouliAccount.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}
	
	public void autocomplete() {
		ZOSearchCondition sc = super.getSearchCondition();
		String keywords = sc.getStr("keywords");
		List<YouliAccount> accountList = YouliAccount.dao.autocomplete(keywords);
		JSONObject j = new JSONObject();
		j.put("type", "success");
		j.put("code", "0");
		j.put("content", accountList);
		renderJson(j);
	}

}
